package com.controller;

import com.mypg.User;
import com.service.Impl.UserServiceImpl;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller

public class UserController {

    @Autowired
    UserService userService;

//    前台登录开始
    @RequestMapping("/hello.do")
    @ResponseBody
    public String doHello(){
        return "hello,springmvc";
    }

    @RequestMapping("/check_login_person.do")
    public ModelAndView doCheck_login_person(HttpSession session){
        Map<String,Object> map=new HashMap<>();
        if(session.getAttribute("user_name")==null){
            map.put("user_name_show","您还没登录！");
        }else{
            map.put("user_name_show",session.getAttribute("user_name"));
        }
        ModelAndView mv=new ModelAndView(new RedirectView("page/person/person.jsp"),map);
        return mv;
    }

    @RequestMapping("/check_login_person_inside.do")
    @ResponseBody
    public int doCheck_login_person_side(HttpSession session){
        int flag=0;
        if(session.getAttribute("user_name")!=null){
            flag=1;
        }
        return flag;
    }

    @RequestMapping("/login.do")
    @ResponseBody
    public int doLogin(String user_name,String password,HttpSession session,HttpServletRequest request){
        User user=userService.selectUserByUser_name(user_name);
        if(user!=null){
            if(password.equals(user.getPassword())){
                session.setAttribute("user_name",user_name);
                return 3;
            }else{
                return 2;
            }
        }else{
            return 1;
        }
    }

    @RequestMapping("/register.do")
    @ResponseBody
    public int doRegister(String user_name,String password,HttpServletRequest request){

        User user=userService.selectUserByUser_name(user_name);
        if(user!=null){
            return 1;
        }else{
            User user1=new User();
            user1.setUser_name(user_name);
            user1.setPassword(password);
            user1.setUser_img("default_user_img.jpg");
            user1.setUser_nickname(user_name);
            user1.setSex(0);
            userService.insertUser(user1);
            return 2;
        }
    }

    @RequestMapping("/out_login.do")
    public ModelAndView doOutLogin(HttpSession session){
        ModelAndView mv=new ModelAndView();
        session.removeAttribute("user_name");
        mv.addObject("user_name_show","您还没登录！");
        mv.setViewName("redirect:page/person/person.jsp");
        return mv;
    }

    @RequestMapping("/change_user_info.do")
    @ResponseBody
    public void doChange_user_info(User user,HttpServletRequest request){
        userService.updateUserByUser_name(user);
    }

    @RequestMapping("/refresh_person_inside.do")
    @ResponseBody
    public User doRefresh_person_inside(HttpServletRequest request,HttpSession session){

        String user_name= (String) session.getAttribute("user_name");
        User user=userService.selectUserByUser_name(user_name);
        return user;
    }

//    前台登录结束

    @RequestMapping("/page_split.do")
    @ResponseBody
    public List<User> doPage_split(@RequestParam(defaultValue = "0")int start_row,
                                   @RequestParam(defaultValue = "5")int page_nums){

       return changeRows(start_row,page_nums);

    }
    public List<User> changeRows(int start_row,int page_nums){
        List<User> userList=userService.selectUserByPage(start_row,page_nums);
        int count=userService.selectUser_count();
        User user=new User();
        user.setUser_id(count);
        userList.add(user);
        return userList;
    }

    @RequestMapping("/delete_user.do")
    @ResponseBody
    public List<User> doDelete_user(int user_id,@RequestParam(defaultValue = "0")int start_row,
                                    @RequestParam(defaultValue = "5")int page_nums){
        userService.deleteUserByUser_id(user_id);
        return changeRows(start_row,page_nums);
    }

    @RequestMapping("/delete_user_many.do")
    @ResponseBody
    public List<User> doDelete_user_many(String[] user_ids, @RequestParam(defaultValue = "0")int start_row,
                                         @RequestParam(defaultValue = "5")int page_nums){
        user_ids[0]=user_ids[0].replace("[","");
        user_ids[user_ids.length-1]=user_ids[user_ids.length-1].replace("]","");
        for(String user_id:user_ids){
            user_id=user_id.replace("\"","");
            if(!"-1".equals(user_id))
              userService.deleteUserByUser_id(Integer.parseInt(user_id));
        }
        return changeRows(start_row,page_nums);
    }

    @RequestMapping("/select_userByUser_name.do")
    @ResponseBody
    public User doSelect_userByUser_name(String user_name){
        User user=userService.selectUserByUser_name(user_name);
        if(user==null){
            user=new User();
            user.setUser_id(-1);
        }
        return user;
    }

}
